*> 100 Cool Things - http://cobolrocks.codeplex.com/
*> Copyright (c) 2009-2010
*> by Emerson Lopes (emersonlopes@gmail.com)
*>
*> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
*> documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
*> the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
*> to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*>
*> The above copyright notice and this permission notice shall be included in all copies or substantial portions 
*> of the Software.
*>
*> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
*> TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
*> THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
*> CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
*> DEALINGS IN THE SOFTWARE.

CLASS-ID. ConcreteIterator as "IteratorPattern.ConcreteIterator"  inherits ClassAbstractIterator. 
    environment division.
        configuration section.
            repository.
                class  ClassObject            as "System.Object"
                class  ClassAbstractIterator  as "IteratorPattern.Iterator"
                class  ClassConcreteAggregate as "IteratorPattern.ConcreteAggregate". 
    
    object.               
        data division.
            working-storage section.
                copy "types.book".
                
                01 _current   type SystemInt32.
                01 _aggregate usage object reference ClassConcreteAggregate private.

        procedure division.
            method-id. NEW.
                data division.
                    linkage section.
                    01    inAggregate  usage object reference ClassConcreteAggregate.
                    
                procedure division using inAggregate.
                    set    _aggregate      to  inAggregate
                    
            end method NEW.
            
            method-id. FirstItem as "First" override.   
                data division.
                    linkage section.
                    01    outItem  usage object reference ClassObject.
                
                procedure division returning outItem.
                    move  zeros     to  _current
                
                    set   outItem   to  _aggregate::"GetItem"(0)
                
            end method FirstItem.
            
            method-id. NextItem as "Next" override.   
                data division.
                    linkage section.
                    01    outItem  usage object reference ClassObject.
                
                procedure division returning outItem.

                    if (_current < _aggregate::"Count"() - 1 )
                        add  1         to  _current
                        set  outItem   to  _aggregate::"GetItem" (_current)  
                    end-if

            end method NextItem.
            
            method-id. CurrentItem as "CurrentItem" override.   
                data division.
                    linkage section.
                    01    outItem  usage object reference ClassObject.
                
                procedure division returning outItem.
                    
                    set   outItem  to  _aggregate::"GetItem"(_current)
         
                
            end method CurrentItem.
            
            method-id. IsDone as "IsDone" override.   
                data division.
                    linkage section.
                    01    outStatus  pic 1. *> boolean
                
                procedure division returning outStatus.
                
                    if (_current >= _aggregate::"Count"())
                       move   b"1"     to  outStatus
                    else
                       move   b"0"     to  outStatus
                    end-if
                                   
            end method IsDone.
            
    end object.         
END CLASS ConcreteIterator.
